package com.vcs.frm.interceptors;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoggingInterceptor implements HandlerInterceptor{

	private Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object object, Exception exception)
			throws Exception {
		System.out.println("This gets executed after Completion.");
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		System.out.println("This method gets executed after our request is handled.");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object object) throws Exception {
		log.info("*******************************************************");
		log.info("** Remote Address : " + request.getRemoteAddr());
		log.info("** Context Path	: " + request.getContextPath());
		log.info("** Request URI	: " + request.getRequestURI());
		Enumeration<String> attrs = request.getAttributeNames();
		while(attrs.hasMoreElements()){
			String attr = attrs.nextElement();
			log.info("** Request Attr	: " + attr +" = "+ request.getAttribute(attr));
		}
		log.info("** Request URI	: " + request.getAttributeNames());
		log.info("*******************************************************");
		return true;
	}

}
